//
//  MeFeedbackVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/15.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "MeFeedbackVC.h"
#import "XLInputTextView.h"
#import "NSString+Ext.h"
@interface MeFeedbackVC ()<UITextViewDelegate>

@property (nonatomic ,strong)XLInputTextView *inputTextV; /**< 地址详情输入框 */

@end

@implementation MeFeedbackVC

- (void)loadView
{
    [super loadView];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"建议反馈";
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.showsVerticalScrollIndicator = NO;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.backgroundColor = xlHexColor(@"#f4f6f9");
    [self.view addSubview:scroll];

    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = xlHexColor(@"#f4f6f9");
    [scroll addSubview:bgV];
    
    UIView *feedBackV = [[UIView alloc] init];
    feedBackV.backgroundColor = [UIColor whiteColor];
    [bgV addSubview:feedBackV];
    [feedBackV addSubview:self.inputTextV];

    UIButton *commitBtn = [UIButton addTarget:self norTitle:@"提交" norColor:[UIColor whiteColor] highTitle:@"提交" highColor:[UIColor whiteColor] titleFont:xlFont(14) backgroundColor:xlHexColor(@"#ffb75d") action:@selector(commitHandler:)];
    commitBtn.layer.cornerRadius = 2;
    commitBtn.layer.masksToBounds = YES;
    [feedBackV addSubview:commitBtn];
    
    UILabel *serviceL = [[UILabel alloc] init];
    serviceL.text = @"客服电话：021-64807818";
    serviceL.textColor = xlHexColor(@"#54698d");
    serviceL.backgroundColor = [UIColor clearColor];
    serviceL.font = xlFont(14);
    serviceL.textAlignment = NSTextAlignmentCenter;
    serviceL.userInteractionEnabled = YES;
    [bgV addSubview:serviceL];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:serviceL action:@selector(tapClick:)];
    [serviceL addGestureRecognizer:tap];
    
    [scroll makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [bgV makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(0);
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        make.bottom.equalTo(serviceL.bottom).offset(20);
    }];
    
    [feedBackV makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(commitBtn.bottom).offset(20);
        make.left.right.equalTo(0);
        make.top.equalTo(16);
    }];
    
    [self.inputTextV makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(10);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(190);
    }];
    
    [commitBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-20);
        make.height.equalTo(32);
        make.width.equalTo(72);
        make.top.equalTo(self.inputTextV.bottom).offset(15);
    }];
    
    [serviceL makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(feedBackV.bottom).offset(24);
        make.height.equalTo(20);
        make.left.equalTo(-16);
        make.right.equalTo(-16);
    }];

    @WeakObj(self);
    self.inputTextV.yz_textHeightChangeBlock = ^(NSString *text,CGFloat textHeight){
        // 文本框文字高度改变会自动执行这个【block】，可以在这【修改底部View的高度】
        // 设置底部条的高度 = 文字高度 + textView距离上下间距约束
        // 为什么添加10 ？（10 = 底部View距离上（5）底部View距离下（5）间距总和）
        [Weakself updateConstraints:textHeight+10];
    };
}

- (void)tapClick:(UITapGestureRecognizer *)ges
{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"021-64807818"];
    // NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
    //        //在这里做你响应return键的代码
    //        [textView resignFirstResponder];
    //        return NO;
    //        //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    //    }
    //
    return YES;
}



- (void)updateConstraints:(CGFloat)height
{
    [self.inputTextV updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(height);
    }];
}


- (void)commitHandler:(UIButton *)btn
{
    
    if ([[NSString deleteForeAndAftSpace:self.inputTextV.text] length] == 0) {
        [self.view makeToast:@"请输入您的建议反馈"];
        return;
    }
    
    NSDictionary *params = @{
                             @"type": @"OSWIFI_FEEDBACK_MOBILE",
                             @"requestEntity": @{
                                     @"feedback": self.inputTextV.text,
                                     @"telephone":[AccountHanler telPhone],
                                     }
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        XLBaseModel *data = [XLBaseModel mj_objectWithKeyValues:returnData];
        
        if (data.resultFlag) {
            
            [Weakself.view makeToast:@"反馈成功"];
        
        }else{
            
            [Weakself.view makeToast:@"反馈成功"];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        [Weakself.view makeToast:@"反馈成功"];
        
    } showHUD:YES];

}

- (XLInputTextView *)inputTextV
{
    if (!_inputTextV) {
        _inputTextV = [[XLInputTextView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, 40)];
        // 设置文本框占位文字
        _inputTextV.placeholder = @"请写下您宝贵的意见和建议，非常感谢";
        _inputTextV.placeholderColor = xlHexColor(@"#a8b7c7");
        // 设置文本框最大行数
        _inputTextV.maxNumberOfLines = 10;
        _inputTextV.backgroundColor = [UIColor whiteColor];
        _inputTextV.delegate = self;
    }
    return _inputTextV;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
